package com.example.tabbar.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.databinding.ItemPermissionBinding
import com.junray.usermangement.room.entity.Permission

class PermissionAdapter : RecyclerView.Adapter<PermissionAdapter.ViewHolder>() {
    private var permissions: MutableList<Permission> = ArrayList()
    private var selectedPositions: MutableSet<Int> = HashSet() // 用于跟踪选中项的位置
    private var selectAllListener: ((Boolean) -> Unit)? = null // 添加全选状态监听器
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PermissionAdapter.ViewHolder {
        val binding = ItemPermissionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PermissionAdapter.ViewHolder, position: Int) {
        val permission = permissions[position]
        holder.checkBox.text = permission.name

        // 移除之前的监听器，避免复用问题
        holder.checkBox.setOnCheckedChangeListener(null)

        // 设置选中状态
        holder.checkBox.isChecked = selectedPositions.contains(position)

        // 设置选择状态改变监听器
        holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                selectedPositions.add(position)
            } else {
                selectedPositions.remove(position)
            }

            // 检查是否需要更新全选按钮状态
            notifySelectAllStateChanged()
        }
    }

    // 全选/取消全选方法
    fun selectAll(select: Boolean) {
        selectedPositions.clear()
        if (select) {
            // 选中所有项
            for (i in 0 until permissions.size) {
                selectedPositions.add(i)
            }
        }
        notifyDataSetChanged()
        selectAllListener?.invoke(select)
    }

    // 检查是否全选
    private fun isAllSelected(): Boolean {
        return selectedPositions.size == permissions.size && permissions.isNotEmpty()
    }
    // 通知全选状态改变
    private fun notifySelectAllStateChanged() {
        val isAllSelected = isAllSelected()
        selectAllListener?.invoke(isAllSelected)
    }
    // 设置全选状态监听器
    fun setSelectAllListener(listener: (Boolean) -> Unit) {
        this.selectAllListener = listener
    }
    override fun getItemCount(): Int {
        return permissions.size
    }
    fun setItems(items: List<Permission>?) {
        permissions.clear()
        if (items != null) {
            permissions.addAll(items)
        }
        notifyDataSetChanged()
    }

    class ViewHolder(binding: ItemPermissionBinding) : RecyclerView.ViewHolder(binding.root) {
        var checkBox = binding.permission
    }
    //获取当前选中的权限
    fun getSelectedPermissions(): List<Permission> {
        return permissions.filterIndexed { index, _ ->
            selectedPositions.contains(index)
        }
    }

    // 在PermissionAdapter.kt中添加以下方法
    fun updateSelectedPositions(positions: Set<Int>) {
        selectedPositions.clear()
        selectedPositions.addAll(positions)
        notifyDataSetChanged()

        // 通知全选状态改变
        notifySelectAllStateChanged()
    }
}